#include "csv_struct.hpp"
#include "labels.hpp"

const csv_struct csv_struct::operator++ (int)
{
        csv_struct t = *this;
        if( first != last)
        {
                parse_info< file_iterator<char> > info = csv_info.parse_csv(first, last);
                if(!info.hit) {
                        std::cout << ERROR_PARSE_POINT<< distance( first, info.stop) << std::endl;
                }
                else { first = info.stop; }
        }

       return t;
}


csv_struct const& csv_struct::operator++()
{
        if( first != last) {
                parse_info< file_iterator < char > > info = csv_info.parse_csv( first, last);
                if(!info.hit)   {
                        std::cout << ERROR_PARSE_POINT << distance( first, info.stop) << std::endl;
                }
                else { first = info.stop; }
        }

        return *this;
}


void csv_struct::check_file_iterator(void)
{
	if(!first)
        {
        	std::cout << CANNOT_OPEN_FILE<< filename << std::endl;
        }
        else
        {
               	last = first.make_end();
                // std::cout << FILENAME<< filename<< std::endl;
        }
}

std::vector<std::string> const& csv_struct::get_line() const
{
	return this->get_csv_parser_info().line();
}

bool csv_struct::eof() const
{
	return first == last;
}

csv_struct::csv_struct(char const* _filename): first(_filename), last(first), csv_info(first)
{
    filename = _filename;
    this->check_file_iterator();
}

csv_struct::csv_struct(std::string const& _filename): first(_filename), last(first), csv_info(first)
{
    filename = _filename;
    this->check_file_iterator();
}

csv_struct const& csv_struct::operator=(csv_struct const& csv_struct)
{
    first = csv_struct.get_file_iterator_first();
    last = csv_struct.get_file_iterator_last();
    csv_info = csv_struct.get_csv_parser_info();

    return *this;
}

csv_struct::~csv_struct()
{
	first = file_iterator<char>();
	last = first;

//	filename.clear();
}
